package com.fractalwrench.harmonybootstrap.sample;

import com.beardedhen.harmonybootstrap.BootstrapButton;
import com.beardedhen.harmonybootstrap.BootstrapButtonGroup;
import com.beardedhen.harmonybootstrap.utils.UiUtil;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapSize;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.Optional;

public class BootstrapButtonGroupExample extends AbilitySlice implements Component.ClickedListener {
    private DefaultBootstrapSize size = DefaultBootstrapSize.MD;

    BootstrapButtonGroup orientationChange;
    BootstrapButtonGroup sizeChange;
    BootstrapButtonGroup outlineChange;
    BootstrapButtonGroup roundedChange;
    BootstrapButtonGroup brandChange;
    BootstrapButtonGroup childChange;
    Button orientationChangeModify;
    Button sizeChangeModify;
    Button outlineChangeModify;
    Button roundedChangeModify;
    Button brandChangeModify;
    Button add,remove;


    Text checkedText;

    BootstrapButton radioButton1;
    BootstrapButton radioButton2;
    BootstrapButton radioButton3;

    private void setShapeElement(Button b) {
        b.setBackground(new UiUtil(getApplicationContext()).getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer)
                LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_example_bootstrap_button_group, null, false);
        orientationChange = (BootstrapButtonGroup) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_orientation_change);
        orientationChangeModify = (Button) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_orientation_change_btn);
        setShapeElement(orientationChangeModify);
        orientationChangeModify.setClickedListener(this);
        sizeChange = (BootstrapButtonGroup) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_size_change);
        sizeChangeModify = (Button) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_size_change_btn);
        setShapeElement(sizeChangeModify);
        sizeChangeModify.setClickedListener(this);
        outlineChange = (BootstrapButtonGroup) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_outline_change);
        outlineChangeModify = (Button) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_outline_change_btn);
        setShapeElement(outlineChangeModify);
        outlineChangeModify.setClickedListener(this);
        roundedChange = (BootstrapButtonGroup) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_rounded_change);
        roundedChangeModify = (Button) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_rounded_change_btn);
        setShapeElement(roundedChangeModify);
        roundedChangeModify.setClickedListener(this);
        brandChange = (BootstrapButtonGroup) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_brand_change);
        brandChangeModify = (Button) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_brand_change_btn);
        setShapeElement(brandChangeModify);
        brandChangeModify.setClickedListener(this);
        childChange = (BootstrapButtonGroup) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_child_change);
        add = (Button) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_child_add_btn);
        setShapeElement(add);
        add.setClickedListener(this);
        remove = (Button) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_child_remove_btn);
        setShapeElement(remove);
        remove.setClickedListener(this);
        checkedText = (Text) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_checked_text);
        radioButton1 = (BootstrapButton) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_checked1);
        radioButton1.setOnCheckedChangedListener(new BootstrapButton.OnCheckedChangedListener() {
            @Override
            public void OnCheckedChanged(BootstrapButton bootstrapButton, boolean isChecked) {
                if (isChecked) {
                    checkedText.setText("radio, button 1 checked");
                }
            }
        });
        radioButton2 = (BootstrapButton) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_checked2);
        radioButton2.setOnCheckedChangedListener(new BootstrapButton.OnCheckedChangedListener() {
            @Override
            public void OnCheckedChanged(BootstrapButton bootstrapButton, boolean isChecked) {
                if (isChecked) {
                    checkedText.setText("radio, button 2 checked");
                }
            }
        });
        radioButton3 = (BootstrapButton) rootLayout.findComponentById(ResourceTable.Id_bbutton_group_checked3);

        radioButton3.setOnCheckedChangedListener(new BootstrapButton.OnCheckedChangedListener() {
            @Override
            public void OnCheckedChanged(BootstrapButton bootstrapButton, boolean isChecked) {
                if (isChecked) {
                    checkedText.setText("radio, button 3 checked");
                }
            }
        });
        super.setUIContent(rootLayout);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bbutton_group_orientation_change_btn:
                onOrientationChangeExampleClicked();
                break;
            case ResourceTable.Id_bbutton_group_outline_change_btn:
                onOutlineChangeExampleClicked();
                break;
            case ResourceTable.Id_bbutton_group_rounded_change_btn:
                onRoundedChangeExampleClicked();
                break;
            case ResourceTable.Id_bbutton_group_child_add_btn:
                try {
                    onChildAddExampleClicked();
                } catch (NotExistException e) {
                    e.printStackTrace();
                } catch (WrongTypeException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case ResourceTable.Id_bbutton_group_child_remove_btn:
                onChildRemoveExampleClicked();
                break;
            case ResourceTable.Id_bbutton_group_brand_change_btn:
                onBrandChangeExampleClicked();
                break;
            case ResourceTable.Id_bbutton_group_size_change_btn:
                onSizeChangeExampleClicked();
                break;
            default:
                break;
        }
    }
    void onOrientationChangeExampleClicked() {
        boolean isHorizontal = orientationChange.getOrientation() == DirectionalLayout.HORIZONTAL;
        int newOrientation = isHorizontal ? DirectionalLayout.VERTICAL : DirectionalLayout.HORIZONTAL;
        orientationChange.setOrientation(newOrientation);
    }

    void onOutlineChangeExampleClicked() {
        outlineChange.setShowOutline(!outlineChange.isShowOutline());
    }

    void onRoundedChangeExampleClicked() {
        roundedChange.setRounded(!roundedChange.isRounded());
    }

    void onChildAddExampleClicked() throws NotExistException, WrongTypeException, IOException {
        int count = childChange.getChildCount();
        BootstrapButton button = new BootstrapButton(this, new AttrSet() {
            @Override
            public Optional<String> getStyle() {
                return Optional.empty();
            }

            @Override
            public int getLength() {
                return 0;
            }

            @Override
            public Optional<Attr> getAttr(int i) {
                return Optional.empty();
            }

            @Override
            public Optional<Attr> getAttr(String s) {
                return Optional.empty();
            }
        });
        button.setText(String.format("%d", count + 1));

        childChange.addComponent(button);
    }

    void onChildRemoveExampleClicked() {
        int count = childChange.getChildCount();

        if (count > 0) {
            childChange.removeComponentAt(count - 1);
        }
    }

    void onBrandChangeExampleClicked() {
        switch ((DefaultBootstrapBrand) brandChange.getBootstrapBrand()) {

            case PRIMARY:
                brandChange.setBootstrapBrand(DefaultBootstrapBrand.SUCCESS);
                break;
            case SUCCESS:
                brandChange.setBootstrapBrand(DefaultBootstrapBrand.INFO);
                break;
            case INFO:
                brandChange.setBootstrapBrand(DefaultBootstrapBrand.WARNING);
                break;
            case WARNING:
                brandChange.setBootstrapBrand(DefaultBootstrapBrand.DANGER);
                break;
            case DANGER:
                brandChange.setBootstrapBrand(DefaultBootstrapBrand.SECONDARY);
                break;
            case SECONDARY:
                brandChange.setBootstrapBrand(DefaultBootstrapBrand.REGULAR);
                break;
            case REGULAR:
                brandChange.setBootstrapBrand(DefaultBootstrapBrand.PRIMARY);
                break;
        }
    }

    void onSizeChangeExampleClicked() {
        switch (size) {
            case XS:
                size = DefaultBootstrapSize.SM;
                break;
            case SM:
                size = DefaultBootstrapSize.MD;
                break;
            case MD:
                size = DefaultBootstrapSize.LG;
                break;
            case LG:
                size = DefaultBootstrapSize.XL;
                break;
            case XL:
                size = DefaultBootstrapSize.XS;
                break;
        }
        sizeChange.setBootstrapSize(size);
    }
}
